package com.sunshine.wrongquestionset.activity.add_question

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import com.sunshine.wrongquestionset.room.entity.QuestionSetEntity
import com.sunshine.wrongquestionset.room.entity.QuestionTypeEntity
import com.sunshine.wrongquestionset.room.entity.SubjectEntity

/**
 * @author sunshine
 * @date 2021/3/29
 * 重写下拉栏适配器
 */
class SpinnerAdapter<T>(
    context: Context,
    private val resource: Int,
    private val textViewResourceId: Int,
    private val list: MutableList<T>,
    private val type: Int
): ArrayAdapter<T>(context, resource, textViewResourceId, list) {

    private var inflater: LayoutInflater = LayoutInflater.from(context)

    override fun getItemId(position: Int): Long {
        return when (type) {
            TYPE_QUESTION_TYPE -> (list[position] as QuestionTypeEntity).id.toLong()
            TYPE_QUESTION_SET -> (list[position] as QuestionSetEntity).id.toLong()
            TYPE_SUBJECT -> (list[position] as SubjectEntity).id.toLong()
            else -> 0
        }
    }

    /**
     * 重写获取显示文字方法
     */
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: inflater.inflate(resource, parent, false)
        val text: TextView
        try {
            if (textViewResourceId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = view as TextView
            } else {
                //  Otherwise, find the TextView field within the layout
                text = view.findViewById(textViewResourceId)
                if (text == null) {
                    throw RuntimeException(
                        "Failed to find view with ID "
                                + context.resources.getResourceName(textViewResourceId)
                                + " in item layout"
                    )
                }
            }
        } catch (e: ClassCastException) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView")
            throw IllegalStateException(
                "ArrayAdapter requires the resource ID to be a TextView", e
            )
        }

        when (type) {
            TYPE_QUESTION_TYPE -> text.text = (list[position] as QuestionTypeEntity).name
            TYPE_QUESTION_SET -> text.text = (list[position] as QuestionSetEntity).name
            TYPE_SUBJECT -> text.text = (list[position] as SubjectEntity).name
        }

        return view
    }

    /**
     * 重写下拉菜单的样式
     */
    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        return getView(position, convertView, parent)
    }

    companion object {
        const val TYPE_QUESTION_TYPE = 0
        const val TYPE_QUESTION_SET = 1
        const val TYPE_SUBJECT = 2
    }

}